from contextlib import contextmanager
from playwright.sync_api import Page
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError

@contextmanager
def track_locator(page,description,selector):
    try:
        page._last_locator = f"{description} (selector: '{selector}')"
        yield
    except PlaywrightTimeoutError as e:
        raise PlaywrightTimeoutError(
            f"{str(e)}\n>>> 失败元素: {page._last_locator}"
        ) from None
    finally:
        page._last_locator = None
